package com.java.al.swing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class ComboBoxes extends JFrame {
	private String[] description = {
		"Ebulliend", "Obtuse", "Recalcitrant", "Brilliant", "Somnescent", "Timorous", "Florid", "Putrescent"	
	};
	private JTextField t = new JTextField(15);
	private WideComboBox c = new WideComboBox();
	private JButton b = new JButton();
	private int count = 0;
	public ComboBoxes(){
		for(int i = 0; i < 4; i++)
			c.addItem(description[count++]);
		t.setEditable(false);
		/**
		 * 点击Button为Box添加选项
		 */
		b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(count < description.length)
					c.addItem(description[count++]);
			}
		});
		/**
		 * 显示选中的选项的索引到TextField中
		 */
		c.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				t.setText("index " + c.getSelectedIndex() + " " +((JComboBox)e.getSource()).getSelectedItem());
			}
		});
		setLayout(new FlowLayout());
		add(t);
		add(c);
		add(b);
	}
	public static void main(String[] args) {
		SwingConsole.run(new ComboBoxes(), 200, 175);
	}

}
